package com.cskaoyan.demo2.servlet;

import com.cskaoyan.demo2.bean.MarketAddress;
import com.cskaoyan.demo2.service.AdminAddressService;
import com.cskaoyan.demo2.util.JsonUtil;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.Part;
import java.io.IOException;
import java.util.List;
import java.util.Map;

/**
 * @Description：
 * @Author：BLUEBLANK
 * @Date：2024/4/8 21:18
 */
// http://39.101.189.16:8083/admin/address/list?page=1&limit=20&sort=add_time&order=desc
@WebServlet("/admin/address/*")
public class AdminAddressServlet extends CommonServlet {

    AdminAddressService adminAddressService = new AdminAddressService();

    public void list(HttpServletRequest request, HttpServletResponse response) throws IOException {
        // 获取请求参数
        int page = Integer.parseInt(request.getParameter("page"));  // 1
        int limit = Integer.parseInt(request.getParameter("limit"));  // 20
        String sort = request.getParameter("sort");  // add_time
        String order = request.getParameter("order").toUpperCase();  // desc 要改大写

        // 假设请求参数不止这些
        // 如何确保参数都拿完了，获取所有的请求参数，然后进行遍历获取
        // Map<String, String[]> parameterMap = request.getParameterMap();
        // for (Map.Entry<String, String[]> entry : parameterMap.entrySet()) {
        //     String key = entry.getKey();
        //
        // }

        // 对数据库进行对应的操作
        int  offset = (page - 1) * limit;
        List<MarketAddress> marketAddressList = adminAddressService.selectByParameter(offset,limit,sort,order);

        // 转换为json字符串
        response.setContentType("application/json;charset=utf-8");
        String jsonStr = JsonUtil.write(marketAddressList);
        response.getWriter().println(jsonStr);


    }

}
